﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.WorkFlow
{
    public partial class WFDefined_StepWiz : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModelID.Text = this.Request.QueryString["ModelID"].ToString();
            this.ActivityType.Text = this.Request.QueryString["ActivityType"].ToString();

            if (!IsPostBack)
            {
                List<SysRoleEntity> roles = new SystemBll().GetAll<SysRoleEntity>("");
                ddlRoles.DataSource = roles;
                ddlRoles.DataTextField = "RoleName";
                ddlRoles.DataValueField = "RoleID";
                ddlRoles.DataBind();
                ddlRoles.Items.Insert(0, new ListItem("全部", ""));

                LoadUser();

                List<WflActivityEntity> wflActivitys = new List<WflActivityEntity>();
                WflActivityBll wflActivityBll = new WflActivityBll();
                wflActivitys = wflActivityBll.GetAll<WflActivityEntity>(" and ModelID = '" + ModelID.Text + "'" + " and ActivityType= '" + ActivityType.Text + "'");
                if (wflActivitys != null && wflActivitys.Count > 0)
                {
                    List<SysUserEntity> users = new List<SysUserEntity>();
                    users = new WorkflowBll().getUsersByActivity(wflActivitys[0].ActivityID);
                    employees.DataSource = users;
                    employees.DataTextField = "UserName";
                    employees.DataValueField = "UserID";
                    employees.DataBind();
                }
            }
        }

        protected void LoadUser()
        {
            SysUserBll userBll = new SysUserBll();
            List<SysUserEntity> userEntityAll = new List<SysUserEntity>();
            string WhereClause = "";
            if (ddlRoles.SelectedValue != "")
            {
                WhereClause = " and UserID in (select UserID from Cms_Sys_UserRoleRelation where RoleID = '" + ddlRoles.SelectedValue + "')";
            }
            userEntityAll = userBll.GetAll<SysUserEntity>(WhereClause," order by UserName");
            employeeChoose.DataSource = userEntityAll;
            employeeChoose.DataTextField = "UserName";
            employeeChoose.DataValueField = "UserID";
            employeeChoose.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            for (int index = 0; index < employeeChoose.Items.Count; index++)
            {
                if (employeeChoose.Items[index].Selected)
                {
                    bool isExist = false;
                    for (int i = 0; i < employees.Items.Count; i++)
                    {
                        if (employeeChoose.Items[index].Value == employees.Items[i].Value)
                        {
                            isExist = true;
                        }
                    }
                    if (!isExist)
                    {
                        ListItem item = new ListItem();
                        item.Text = employeeChoose.Items[index].Text;
                        item.Value = employeeChoose.Items[index].Value;
                        employees.Items.Add(item);
                    }
                }
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            List<ListItem> items = new List<ListItem>();
            for (int index = 0; index < employees.Items.Count; index++)
            {
                if (!employees.Items[index].Selected)
                {
                    items.Add(new ListItem(employees.Items[index].Text, employees.Items[index].Value));
                }
            }
            employees.Items.Clear();
            for (int index = 0; index < items.Count; index++)
            {
                employees.Items.Add(items[index]);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (employees.Items.Count > 0)
            {
                List<WflActivityEntity> wflActivitys = new List<WflActivityEntity>();
                WflActivityBll wflActivityBll = new WflActivityBll();
                wflActivitys = wflActivityBll.GetAll<WflActivityEntity>(" and ModelID = '" + ModelID.Text + "'" + " and ActivityType= '" + ActivityType.Text + "'");
                if (wflActivitys != null && wflActivitys.Count > 0)
                {
                    List<SqlDbParameter> parms = new List<SqlDbParameter>();
                    SqlDbParameter parm = new SqlDbParameter();
                    parm.ColumnName = "ActivityID";
                    parm.ColumnType = System.Data.DbType.Guid;
                    parm.ParameterName = "@ActivityID";
                    parm.ParameterValue = wflActivitys[0].ActivityID;
                    parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                    parms.Add(parm);
                    new WflActivityCheckersBll().Delete<WflActivityCheckersEntity>(parms);

                    for (int index = 0; index < employees.Items.Count; index++)
                    {
                        List<WflActivityCheckersEntity> activityCheckerExists = new WflActivityCheckersBll().GetAll<WflActivityCheckersEntity>(" and ActivityID = '" + wflActivitys[0].ActivityID.ToString() + "' and UserID = '" + employees.Items[index].Value + "'");
                        if (activityCheckerExists != null && activityCheckerExists.Count > 0)
                        {
                            continue;
                        }

                        WflActivityCheckersEntity activityChecker = new WflActivityCheckersEntity();
                        activityChecker.ID = Guid.NewGuid();
                        SysUserEntity userEntity = new SysUserBll().Get<SysUserEntity>(new Guid(employees.Items[index].Value));
                        activityChecker.Account = userEntity.Account;
                        activityChecker.ActivityID = wflActivitys[0].ActivityID;
                        activityChecker.UserID = userEntity.UserID;
                        activityChecker.UserName = userEntity.UserName;
                        new WflActivityCheckersBll().Insert(activityChecker);
                        ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('请选择人员');", true);
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            LoadUser();
        }
    }
}
